package com.nwcl.basic.service.impl;

import com.nwcl.basic.bo.OrdersDetailBO;
import com.nwcl.basic.dao.OrdersDetailDao;
import com.nwcl.basic.entity.OrdersDetail;
import com.nwcl.basic.service.OrdersDetailService;
import com.nwcl.common.utils.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author liguiqin
 * @date 2023/5/26
 */
@Service
public class OrdersDetailServiceImpl implements OrdersDetailService {
    @Autowired
    private OrdersDetailDao ordersDetailDao;
    @Override
    public void save(OrdersDetailBO ordersDetailBO) {
        OrdersDetail ordersDetail = BeanUtil.copy(ordersDetailBO, OrdersDetail.class);
        ordersDetailDao.save(ordersDetail);
    }

    @Override
    public List<OrdersDetailBO> listByOrderIds(List<Integer> orderIds) {
        List<OrdersDetail> ordersDetailsList = ordersDetailDao.listByOrderIds(orderIds);
        if (ordersDetailsList != null) {
            return BeanUtil.copyList(ordersDetailsList, OrdersDetailBO.class);
        }
        return Collections.emptyList();
    }

    @Override
    public Map<Integer, List<OrdersDetailBO>> mapByOrderIds(List<Integer> orderIds) {
        List<OrdersDetailBO> ordersDetailBOList = this.listByOrderIds(orderIds);
        Map<Integer, List<OrdersDetailBO>> map=new HashMap<>();
        for(OrdersDetailBO ordersDetailBO:ordersDetailBOList){
            List<OrdersDetailBO> list=map.getOrDefault(ordersDetailBO.getOrderId(),new ArrayList<>());
            list.add(ordersDetailBO);
            map.put(ordersDetailBO.getOrderId(),list);
        }
        return map;
    }
}
